package com.woniu.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.Addr;
import com.woniu.common.utils.ResponseData;
import com.woniu.order.dto.EchartsDataDto1;
import com.woniu.order.dto.OrderDetailDTO;
import com.woniu.order.feign.UserFeign;
import com.woniu.common.dto.ForOrderProductDTO;
import com.woniu.common.dto.payOrderDetailDTO;
import com.woniu.common.utils.ResponseData;
import com.woniu.order.feign.ProductFeign;
import com.woniu.order.pojo.OrderDetail;
import com.woniu.order.service.OrderDetailService;
import com.woniu.order.mapper.OrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ltyipx
* @description 针对表【order_detail(订单详情表)】的数据库操作Service实现
* @createDate 2024-08-07 09:44:03
*/
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
    implements OrderDetailService{
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ProductFeign productFeign;

    @Override
    public ResponseData<?> queryOrderDetailByOrderId(String orderId) {
        List<OrderDetail> list = lambdaQuery().eq(OrderDetail::getOrderId, orderId).list();
        List<String> idss = list.stream().map(OrderDetail::getProductId).collect(Collectors.toList());
        String[] ids = idss.stream().toArray(String[]::new);
        ResponseData<List<ForOrderProductDTO>> data = productFeign.queryGoodsByIds(ids);
        List<ForOrderProductDTO> products = data.getData();
        ArrayList<payOrderDetailDTO> payOrderDetailDTOS = new ArrayList<>();
        for (ForOrderProductDTO product : products) {
            payOrderDetailDTO payOrderDetailDTO = new payOrderDetailDTO();
            for (OrderDetail orderDetail : list) {
                if(orderDetail.getProductId().equals(product.getId())){
                    payOrderDetailDTO.setId(orderDetail.getId());
                    payOrderDetailDTO.setOrderId(orderDetail.getOrderId());
                    payOrderDetailDTO.setProductId(product.getId());
                    payOrderDetailDTO.setProductImg(product.getProductImg());
                    payOrderDetailDTO.setBuyNum(orderDetail.getBuyNum());
                    payOrderDetailDTO.setProductName(product.getName());
                    payOrderDetailDTO.setProductPrice(product.getPrice());
                }
            }
            payOrderDetailDTOS.add(payOrderDetailDTO);
        }

        return new ResponseData<>().success(payOrderDetailDTOS);
    }

    @Override
    public ResponseData<?> queryOrderDetailTypeNumber() {
        List<EchartsDataDto1> list = orderDetailMapper.queryOrderDetailTypeNumber();
        return new ResponseData<>().success(list);
    }

    @Override
    public ResponseData<?> payOrderDetailNumber() {
        Integer number = orderDetailMapper.payOrderDetailNumber();
        return new ResponseData<>().success(number);
    }

    @Autowired
    private UserFeign userFeign;
    @Override
    public ResponseData<?> queryInvoice(String orderId) {
        List<OrderDetail> orderDetails = lambdaQuery().eq(OrderDetail::getOrderId, orderId)
                .list();
        Addr addr = userFeign.selectAddr(orderDetails.get(0).getAddressId());
        OrderDetailDTO orderDetailDTO = new OrderDetailDTO();
        orderDetailDTO.setOrderDetails(orderDetails);
        orderDetailDTO.setAddress(addr);
        return new ResponseData<>().success(orderDetailDTO);
    }
}




